﻿using Sunny.UI;

namespace JFBG
{
    public partial class FAddForm : UIEditForm
    {
        public FAddForm ()
        {
            InitializeComponent();
        }

        public bool IsOK { get; private set; }

        public event EventHandler ButtonOkClick;
        public event EventHandler ButtonCancelClick;

        protected override bool CheckData()
        {
            return CheckEmpty(tbSensorName, "请输入传感器名称")
                   && CheckEmpty(cbSensorType, "请输入传感器类型")
                   && CheckRange(tbChannelIndex, 0, 15, "请输入通道号，范围0~15")
                   && CheckRange(tbSensorIndex, 0, 29, "请输入传感器号，范围0~29")
                   && CheckRange(tbWaveCount, 0, 29, "请输入波长数量，范围1~4")
                   && CheckEmpty(tbExpression, "请输入传感器物理值计算表达式");
        }

        protected override void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }

            if (ButtonOkClick != null)
            {
                ButtonOkClick.Invoke(sender, e);
            }
            else
            {
                DialogResult = DialogResult.OK;
                IsOK = true;
                Close();
            }
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            if (ButtonCancelClick != null)
            {
                ButtonCancelClick.Invoke(sender, e);
            }
            else
            {
                DialogResult = DialogResult.Cancel;
                IsOK = false;
                Close();
            }
        }

        private AddRaster addRaster;

        public AddRaster AddRaster
        {
            get
            {
                if (addRaster == null)
                {
                    addRaster = new AddRaster();
                }

                addRaster.SensorName = tbSensorName.Text;
                addRaster.SensorType = cbSensorType.SelectedIndex;
                addRaster.ChannelIndex = tbChannelIndex.IntValue;
                addRaster.SensorIndex = tbSensorIndex.IntValue;
                addRaster.WaveCount = tbWaveCount.IntValue;
                addRaster.Expression = tbExpression.Text;
                addRaster.Description = tbDescription.Text;
                addRaster.Param1 = tbParam1.Text;
                addRaster.Param2 = tbParam2.Text;
                addRaster.Param3 = tbParam3.Text;
                addRaster.Param4 = tbParam4.Text;
                addRaster.Param5 = tbParam5.Text;
                addRaster.Param6 = tbParam6.Text;
                addRaster.Param7 = tbParam7.Text;
                addRaster.Param8 = tbParam8.Text;
                addRaster.Param9 = tbParam9.Text;
                addRaster.Param10 = tbParam10.Text;
                addRaster.Param11 = tbParam11.Text;
                addRaster.Param12 = tbParam12.Text;
                addRaster.Param13 = tbParam13.Text;
                addRaster.Param14 = tbParam14.Text;
                addRaster.Param15 = tbParam15.Text;
                addRaster.Param16 = tbParam16.Text;

                return addRaster;
            }

            set
            {
                addRaster = value;
                tbSensorName.Text = value.SensorName;
                cbSensorType.SelectedIndex = cbSensorType.Items.IndexOf(value.SensorType);
                tbChannelIndex.IntValue = value.ChannelIndex;
                tbSensorIndex.IntValue = value.SensorIndex;
                tbWaveCount.IntValue = value.WaveCount;
                tbExpression.Text = value.Expression;
                tbDescription.Text = value.Description;
                tbParam1.Text = value.Param1;
                tbParam2.Text = value.Param2;
                tbParam3.Text = value.Param3;
                tbParam4.Text = value.Param4;
                tbParam5.Text = value.Param5;
                tbParam6.Text = value.Param6;
                tbParam7.Text = value.Param7;
                tbParam8.Text = value.Param8;
                tbParam9.Text = value.Param9;
                tbParam10.Text = value.Param10;
                tbParam11.Text = value.Param11;
                tbParam12.Text = value.Param12;
                tbParam13.Text = value.Param13;
                tbParam14.Text = value.Param14;
                tbParam15.Text = value.Param15;
                tbParam16.Text = value.Param16;
            }
        }
    }
}

public class AddRaster
{
    public string SensorName { get; set; }

    public int SensorType { get; set; }

    public int ChannelIndex { get; set; }

    public int SensorIndex { get; set; }

    public int WaveCount { get; set; }

    public string Expression { get; set; }

    public string Description { get; set; }

    public string Param1 { get; set; }

    public string Param2 { get; set; }

    public string Param3 { get; set; }

    public string Param4 { get; set; }

    public string Param5 { get; set; }

    public string Param6 { get; set; }

    public string Param7 { get; set; }

    public string Param8 { get; set; }

    public string Param9 { get; set; }

    public string Param10 { get; set; }

    public string Param11 { get; set; }

    public string Param12 { get; set; }

    public string Param13 { get; set; }

    public string Param14 { get; set; }

    public string Param15 { get; set; }

    public string Param16 { get; set; }

    public override string ToString()
    {
        return SensorName + ", " + SensorType + ", " + ChannelIndex + ", " + SensorIndex + ", " + WaveCount + ", " + Expression + ", " + Description 
                          + ", " + Param1 + ", " + Param2 + ", " + Param3 + ", " + Param4 + ", " + Param5 + ", " + Param6 + ", " + Param7 + ", " + Param8
                          + ", " + Param9 + ", " + Param10 + ", " + Param11 + ", " + Param12 + ", " + Param13 + ", " + Param14 + ", " + Param15 + ", " + Param16;
    }
}